package com.jianguozh.difs.support.service.listener;

import cn.hutool.core.bean.BeanUtil;
import com.jianguozh.difs.framework.event.WriteLogEvent;
import com.jianguozh.difs.framework.model.OperateLog;
import com.jianguozh.difs.support.service.base.entity.Ulog;
import com.jianguozh.difs.support.service.base.service.IUlogService;
import com.jianguozh.difs.support.service.sys.entity.Client;
import com.jianguozh.difs.support.service.sys.service.IClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 写操作日志事件监听
 *
 * @author hanrs
 * @email 4076130@qq.com
 * @since 2022/6/9 18:58
 */
@Component
public class WriteLogListener {

    @Autowired
    IClientService clientService;

    @Autowired
    IUlogService ulogService;

    @Async
    @Order
    @EventListener(WriteLogEvent.class)
    public void saveLog(WriteLogEvent event) {
        OperateLog operateLog = (OperateLog) event.getSource();
        Ulog ulog = new Ulog();
        BeanUtil.copyProperties(operateLog, ulog);
        Client client = clientService.getById(ulog.getAppId());
        if (null != client) {
            ulog.setAppId(client.getAppId());
        }
        ulogService.save(ulog);
    }

}
